package com.beten.aclear;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;
import android.content.Intent;
import android.os.Process;

import com.beten.util.DebugHelp;

public class CrashReportHandler implements UncaughtExceptionHandler {

	public static void attach(Context context) {
		Thread.setDefaultUncaughtExceptionHandler(
			new CrashReportHandler(context)
		);
	}
	
	///////////////////////////////////////////// implementation
	
	private CrashReportHandler(Context context) {
		m_context=context;
	}
	
	public void uncaughtException(Thread thread,Throwable exception) {
		boolean isDebug = DebugHelp.IsDebug();
		 
		if(isDebug){
			StringWriter stackTrace = new StringWriter();
			exception.printStackTrace(new PrintWriter(stackTrace));

			Intent intent = new Intent(m_context, CrashReportActivity.class);
			DebugHelp.LogE("CrashReportHandler", stackTrace.toString());
			intent.putExtra(CrashReportActivity.EXTRA_STACKTRACE,
					stackTrace.toString());
			m_context.startActivity(intent);

			DebugHelp.LogE("MVQQ", stackTrace.toString());
		}
		// from RuntimeInit.crash()
		Process.killProcess(Process.myPid());
		System.exit(10);
	}

	private Context m_context;
}
